package com.ljw.community.service.impl;

import com.ljw.community.entity.User;
import com.ljw.community.mapper.UserMapper;
import com.ljw.community.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public String register(User user) {
        User dbUser = userMapper.getByLoginName(user.getLoginName());
        if (dbUser != null) {
            return "登录名已存在";
        }
        //执行插入
        String id = UUID.randomUUID().toString().replace("-", "");     //生成随机数id
        user.setId(id);
        user.setCreateTime(new Date());

        int rowNum = userMapper.insert(user);

        return rowNum == 1 ? "注册成功，等待管理员审核" : "error";
    }

    @Override
    public User getInfo(String loginName) {
        return userMapper.getByLoginName(loginName);
    }

    @Override
    public int update(User user) {
        return userMapper.updateByLoginName(user);
    }

    @Override
    public List<User> loadUnverifiedUser(int curr, int limit) {
        int startIndex = curr * limit - limit;
        return userMapper.loadUnverifiedUser(startIndex, limit);
    }

    @Override
    public List<User> loadUserList(int curr, int limit, String query) {
        int startIndex = curr * limit - limit;
        if (query == null) {
            query = "";
        }
        return userMapper.loadUserList(startIndex, limit, query);
    }
}
